﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Mime;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Quaack.Properties;
using TextBox = System.Web.UI.WebControls.TextBox;

namespace Quaack
{
    public partial class sendpw : System.Web.UI.Page
    {


        protected void btnSendPwd_Click(object sender, EventArgs e)

        {
            String inputUser = txtUserName.Text;

            SqlConnection sqlConn = new SqlConnection(Settings.Default.quaackdb);

            String dbMail = "";
            SqlCommand getMail = new SqlCommand("Select email from profile where username = '" + inputUser + "'", sqlConn);

            sqlConn.Open();
            object sqlObjectMail = getMail.ExecuteScalar();
            sqlConn.Close();

            if (sqlObjectMail == null)

            {
                //foute of onbekende username
                lblFeedback.Text = "'" + inputUser + "' komt niet voor als username in de database";
            }
        

            else
            {
                dbMail = Convert.ToString(sqlObjectMail.ToString());
                Session.Add("DbMail", dbMail);

                String dbPwd = "";
                SqlCommand getPwd = new SqlCommand("Select password from profile where username = '" + inputUser + "'",
                    sqlConn);
                sqlConn.Open();
                object sqlObjectPwd = getPwd.ExecuteScalar();
                sqlConn.Close();
                dbPwd = Convert.ToString(sqlObjectPwd.ToString());
                Session.Add("DbPwd", dbPwd);
                //email
                string emailAddress = dbMail;
                //string recipient = Convert.ToString(emailAddress);
                string title = "Uw wachtwoord van quaack";
                string body = "Beste '" + inputUser + "'. Uw wachtwoord is: " + dbPwd;
                Email sendPwd = new Email();
                sendPwd.SendMail(emailAddress, title, body);
                lblFeedback.Text = ("Het wachtwoord is verzonden naar het emailadres dat behoort bij het door u opgegeven emailadres.");
            }

        }
    }
}